package org.siberia.server.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.siberia.client.service.TemplateEngineService;
import org.siberia.shared.FieldVerifier;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class TemplateEngineServiceImpl extends RemoteServiceServlet implements TemplateEngineService
{

	public List<String> getAllCalendars(String login, String password)
	{
		List<String> result = null;
		
		CalendarService myService = new CalendarService("gcal-templated");
		try
		{
			myService.setUserCredentials(login, password);
			
			URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
			CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
			int size = resultFeed.getEntries().size();
			for (int i = 0; i < size; i++)
			{
				CalendarEntry entry = resultFeed.getEntries().get(i);
				if ( result == null )
				{
					result = new ArrayList<String>(size);
				}
				result.add(entry.getTitle().getPlainText());
			}
		}
		catch(AuthenticationException e)
		{
			e.printStackTrace();
		}
		catch(ServiceException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		
		if ( result == null )
		{
			result = Collections.<String>emptyList();
		}
		
		return result;
	}
	
	public String templateMyAgenda(String login, String password, String agendaName, Date startDateIncluded, Date endDateIncluded, Long templateId)
	{
		String result = null;
		
		System.setProperty("http.proxyHost", "cie-vproxy2.cie.etat.lu");
		System.setProperty("http.proxyPort", "80");
		System.setProperty("https.proxyHost", "cie-vproxy2.cie.etat.lu");
		System.setProperty("https.proxyPort", "80");
		
		CalendarService myService = new CalendarService("gcal-templated");
		try
		{
			CalendarEventEntry event = new CalendarEventEntry();
			//event.getContent().
			
			myService.setUserCredentials(login, password);
			
			URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
			CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
			int size = resultFeed.getEntries().size();
			for (int i = 0; i < size; i++)
			{
				CalendarEntry entry = resultFeed.getEntries().get(i);
				if ( result == null )
				{
					//result = new ArrayList<String>(size);
				}
				
				//result.add(entry.getTitle().getPlainText());
			}
		}
		catch(AuthenticationException e)
		{
			e.printStackTrace();
		}
		catch(ServiceException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		
		//getServletContext().g
		
		return result;
		
//		this.get
		
//		// Verify that the input is valid. 
//		if (!FieldVerifier.isValidName(input)) {
//			// If the input is not valid, throw an IllegalArgumentException back to
//			// the client.
//			throw new IllegalArgumentException(
//					"Name must be at least 4 characters long");
//		}
//
//		String serverInfo = getServletContext().getServerInfo();
//		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
//
//		// Escape data from the client to avoid cross-site script vulnerabilities.
//		input = escapeHtml(input);
//		userAgent = escapeHtml(userAgent);
//
//		return "Hello, " + input + "!<br><br>I am running " + serverInfo
//				+ ".<br><br>It looks like you are using:<br>" + userAgent;
		//return null;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
}
